//
//  ZCXNewPetDetialTableViewCell.m
//  LoveOfPet
//
//  Created by dllo on 15/11/16.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "ZCXNewPetDetialTableViewCell.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>
#define HEIGHT  [[UIScreen mainScreen]bounds].size.height / 667
#define WIDTH   [[UIScreen mainScreen]bounds].size.width / 375
@interface ZCXNewPetDetialTableViewCell ()

@property (nonatomic, retain) UIImageView *userImageV;
@property (nonatomic, retain) UILabel *userLabel;
@property (nonatomic, retain) UILabel *feelLabel;
@property (nonatomic, retain) UIView *backView;
//@property (nonatomic, retain) AVPlayerLayer *playerLayer;
@property (nonatomic, copy) NSString *strID;
@property (nonatomic, assign) BOOL sign;
@end

@implementation ZCXNewPetDetialTableViewCell
- (void)dealloc{
    [_userImageV release];
    [_userLabel release];
    [_feelLabel release];
    [_contactView release];
    [_mvModel release];
    [_backView release];
//    [_play release];
    [_pauseImageV release];
    [_strID release];
    [_delegate release];
//    [_playerLayer release];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
//    Block_release(_myBlock);
    [super dealloc];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.fun = NO;
       
        self.userImageV = [[UIImageView alloc]init];
        _userImageV.layer.masksToBounds = YES;
        _userImageV.layer.cornerRadius = 20 * WIDTH;
        _userImageV.userInteractionEnabled = YES;
        [self.contentView addSubview:_userImageV];
        [_userImageV release];
        
        UITapGestureRecognizer *imageTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushIntoUserDetial:)];
        [_userImageV addGestureRecognizer:imageTap];
        [imageTap release];
        
        self.userLabel = [[UILabel alloc]init];
        _userLabel.font = [UIFont systemFontOfSize:15 * WIDTH];
        _userLabel.textColor = [UIColor grayColor];
        [self.contentView addSubview:_userLabel];
        [_userLabel release];
        
        self.feelLabel = [[UILabel alloc]init];
        _feelLabel.font = [UIFont systemFontOfSize:15 * WIDTH];
        _feelLabel.numberOfLines = 0;
        [self.contentView addSubview:_feelLabel];
        [_feelLabel release];
        
        self.backView = [[UIView alloc]init];
        [self.contentView addSubview:_backView];
        [_backView release];
        
        self.contactView = [[UIImageView alloc]init];
        _contactView.backgroundColor = [UIColor blackColor];
        [self.contentView addSubview:_contactView];
        [_contactView release];
        
        self.pauseImageV = [[UIImageView alloc]init];
        _pauseImageV.image = [UIImage imageNamed:@"Zpause"];
        [self.contentView addSubview:_pauseImageV];
        [_pauseImageV release];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(playOrPause:)];
        [_backView addGestureRecognizer:tap];
        [tap release];
//        self.play = [[AVPlayer alloc]init];
//        self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:_play];
//        
//        _playerLayer.backgroundColor = [UIColor yellowColor].CGColor;
//        [_backView.layer addSublayer:_playerLayer];
//        [_playerLayer release];
//        [_play release];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    _userImageV.frame = CGRectMake(5 * WIDTH, 5 * HEIGHT, 40 * WIDTH, 40 * HEIGHT);
    _userLabel.frame = CGRectMake(10 * WIDTH + _userImageV.frame.size.width, 5 * HEIGHT, 200 * WIDTH, 20 * HEIGHT);
    _contactView.frame = CGRectMake(0, 10 * HEIGHT + _userImageV.frame.size.height, self.frame.size.width, self.frame.size.height - 95 * HEIGHT);
    
    _backView.frame = CGRectMake(0, 10 * HEIGHT + _userImageV.frame.size.height, self.frame.size.width, self.frame.size.height - 95 * HEIGHT);
    _backView.backgroundColor = [UIColor blackColor];
    
    _feelLabel.frame = CGRectMake(5 * WIDTH, self.frame.size.height - 40 * HEIGHT, self.frame.size.width - 10 * WIDTH, 36 * HEIGHT);
    _pauseImageV.frame = CGRectMake(self.frame.size.width / 2 - 50 * WIDTH, self.frame.size.height / 2 - 50 * HEIGHT, 100 * WIDTH, 100 * HEIGHT);
    
//    _playerLayer.frame = CGRectMake(0, 0, _backView.frame.size.width, _backView.frame.size.height);
}
- (void)setMvModel:(ZCXMVModel *)mvModel{
    if (_mvModel != mvModel) {
        [_mvModel release];
        _mvModel = [mvModel retain];
    }
    
//    AVPlayerItem *playeritamr = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:mvModel.video]];
//    [_play replaceCurrentItemWithPlayerItem:playeritamr];
    
//    _play = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:mvModel.video]];
//
    
//    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_play];
//    playerLayer.frame = CGRectMake(0, 0, _backView.frame.size.width, _backView.frame.size.height);
//    playerLayer.backgroundColor = [UIColor clearColor].CGColor;
//    [_backView.layer addSublayer:playerLayer];
//    [self addNotification];

    [_userImageV sd_setImageWithURL:[NSURL URLWithString:[mvModel.user objectForKey:@"avatar"]]];
    _userLabel.text = [mvModel.user objectForKey:@"screen_name"];
    _feelLabel.textColor = [UIColor grayColor];
    _feelLabel.text = mvModel.caption;
    [_contactView sd_setImageWithURL:[NSURL URLWithString:mvModel.cover_pic]];
     self.strID = [mvModel.user objectForKey:@"id"];
//    self.myBlock(mvModel.video);
   
}
- (void)playOrPause:(UIGestureRecognizer *)tap{
//    NSLog(@"🍎%@", self.mvModel.video);
//    if (self.sign == NO && self.fun == YES) {
//        NSLog(@"🍌%@", self.mvModel.video);
//        self.play = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:self.mvModel.video]];
//        AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_play];
//        playerLayer.frame = CGRectMake(0, 0, _backView.frame.size.width, _backView.frame.size.height);
//        playerLayer.backgroundColor = [UIColor blackColor].CGColor;
//        [_backView.layer addSublayer:playerLayer];
//        [self addNotification];
//        self.sign = YES;
//        self.fun = NO;
//        [_play release];
//    }
//    if (self.fun == NO) {
//        _pauseImageV.hidden = YES;
//        _contactView.hidden = YES;
//        [_play play];
//        _fun = YES;
//    }else{
//        _pauseImageV.hidden = NO;
//        [_play pause];
//        _fun = NO;
//    }
  [self.delegate valueWithAVplayer:self.mvModel];
}
//-(void)addNotification
//{
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playDidFinish) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
//}
//-(void)playDidFinish
//{
////    NSLog(@"播放完成");
////    _sign = NO;
////    _fun = YES;
////    _contactView.hidden = NO;
////    [self playOrPause:nil];
//////    [self setMvModel:_mvModel];
//}
- (void)pushIntoUserDetial:(UIGestureRecognizer *)tap{
    [self.delegate pushIntoUser:self.strID];
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
